package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.base.SqlResult;
import com.zerowidth.db.bean.VipEnumBean;
import com.zerowidth.db.utils.IOUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VipEnumDB {

    public static List<String> getMockVipDescList(String channel, boolean onlyUnbind) {
        int descType = 0;
        if (onlyUnbind) {
            descType = 1;
        }
        String sql = String.format("SELECT * FROM mock_vip_desc WHERE desc_type = %d " +
                " AND desc_status = 0 AND (channel = '%s' OR channel IS NULL) ORDER BY id ASC", descType, channel);

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            List<String> mockVipDescList = new ArrayList<>();
            while (rs.next()) {
                mockVipDescList.add(rs.getString("vip_desc"));
            }

            return mockVipDescList;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

    public static List<VipEnumBean> getVipEnumList_ShunFeng(boolean onlyUnbind) {
        String sql = "";
        String unbindVipType = DataConfigDB.getValue("unbind_vip_type");

        if (onlyUnbind) {
            sql = String.format("SELECT * FROM vip_enum WHERE vip_type = %s AND vip_enable=1 ORDER BY order_value", unbindVipType);
        } else {
            sql = String.format("SELECT * FROM vip_enum WHERE vip_type = 7 AND vip_enable=1 ORDER BY order_value");
        }

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            List<VipEnumBean> vipEnumBeanList = new ArrayList<>();
            while (rs.next()) {
                VipEnumBean bean = new VipEnumBean();
                bean.id = rs.getInt("id");
                bean.vip_type = rs.getInt("vip_type");
                bean.vip_name = rs.getString("vip_name");
                bean.vip_enable = rs.getBoolean("vip_enable");
                bean.vip_desc = rs.getString("vip_desc");
                bean.price = rs.getFloat("price");
                bean.price_unit = rs.getString("price_unit");
                bean.discount_price = rs.getFloat("discount_price");
                bean.discount_price_unit = rs.getString("discount_price_unit");
                bean.discount_enable = rs.getBoolean("discount_enable");
                bean.product_code = rs.getString("product_code");
                bean.vip_view_color = rs.getString("vip_view_color");
                bean.vip_duration = rs.getLong("vip_duration");
                vipEnumBeanList.add(bean);
            }

            return vipEnumBeanList;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

    /**
     * 鹰眼定位的会员价格
     */
    public static List<VipEnumBean> getVipEnumList_Eye(boolean onlyUnbind) {
        String sql = "";
        String unbindVipType = DataConfigDB.getValue("unbind_vip_type");

        if (onlyUnbind) {
            sql = String.format("SELECT * FROM vip_enum WHERE vip_type = %s AND vip_enable=1 ORDER BY order_value", unbindVipType);
        } else {
            sql = String.format("SELECT * FROM vip_enum WHERE vip_type <> 7 AND vip_type <> 20000 AND vip_type <> %s AND vip_enable=1 ORDER BY order_value", unbindVipType);
        }

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            List<VipEnumBean> vipEnumBeanList = new ArrayList<>();
            while (rs.next()) {
                VipEnumBean bean = new VipEnumBean();
                bean.id = rs.getInt("id");
                bean.vip_type = rs.getInt("vip_type");
                bean.vip_name = rs.getString("vip_name");
                bean.vip_enable = rs.getBoolean("vip_enable");
                bean.vip_desc = rs.getString("vip_desc");
                bean.price = rs.getFloat("price");
                bean.price_unit = rs.getString("price_unit");
                bean.discount_price = rs.getFloat("discount_price");
                bean.discount_price_unit = rs.getString("discount_price_unit");
                bean.discount_enable = rs.getBoolean("discount_enable");
                bean.product_code = rs.getString("product_code");
                bean.vip_view_color = rs.getString("vip_view_color");
                bean.vip_duration = rs.getLong("vip_duration");
                vipEnumBeanList.add(bean);
            }

            return vipEnumBeanList;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }



    public static List<VipEnumBean> getVipEnumList_DingSafe() {
        String sql = "";
        sql = String.format("SELECT * FROM vip_enum WHERE vip_type = 20000 AND vip_enable=1 ORDER BY order_value");

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            List<VipEnumBean> vipEnumBeanList = new ArrayList<>();
            while (rs.next()) {
                VipEnumBean bean = new VipEnumBean();
                bean.id = rs.getInt("id");
                bean.vip_type = rs.getInt("vip_type");
                bean.vip_name = rs.getString("vip_name");
                bean.vip_enable = rs.getBoolean("vip_enable");
                bean.vip_desc = rs.getString("vip_desc");
                bean.price = rs.getFloat("price");
                bean.price_unit = rs.getString("price_unit");
                bean.discount_price = rs.getFloat("discount_price");
                bean.discount_price_unit = rs.getString("discount_price_unit");
                bean.discount_enable = rs.getBoolean("discount_enable");
                bean.product_code = rs.getString("product_code");
                bean.vip_view_color = rs.getString("vip_view_color");
                bean.vip_duration = rs.getLong("vip_duration");
                vipEnumBeanList.add(bean);
            }

            return vipEnumBeanList;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

    public static List<VipEnumBean> getVipEnumList(boolean onlyUnbind) {
        String sql = "";
        String unbindVipType = DataConfigDB.getValue("unbind_vip_type");
        if (onlyUnbind) {
            sql = String.format("SELECT * FROM vip_enum WHERE vip_type = %s AND vip_enable=1 ORDER BY order_value", unbindVipType);
        } else {
            sql = String.format("SELECT * FROM vip_enum WHERE vip_type <> %s AND vip_enable=1 ORDER BY order_value", unbindVipType);
        }

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            List<VipEnumBean> vipEnumBeanList = new ArrayList<>();
            while (rs.next()) {
                VipEnumBean bean = new VipEnumBean();
                bean.id = rs.getInt("id");
                bean.vip_type = rs.getInt("vip_type");
                bean.vip_name = rs.getString("vip_name");
                bean.vip_enable = rs.getBoolean("vip_enable");
                bean.vip_desc = rs.getString("vip_desc");
                bean.price = rs.getFloat("price");
                bean.price_unit = rs.getString("price_unit");
                bean.discount_price = rs.getFloat("discount_price");
                bean.discount_price_unit = rs.getString("discount_price_unit");
                bean.discount_enable = rs.getBoolean("discount_enable");
                bean.product_code = rs.getString("product_code");
                bean.vip_view_color = rs.getString("vip_view_color");
                bean.vip_duration = rs.getLong("vip_duration");
                vipEnumBeanList.add(bean);
            }

            return vipEnumBeanList;
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

    public static VipEnumBean findVipEnum(int vipType) {
        String sql = String.format("SELECT * FROM vip_enum WHERE vip_type=%d", vipType);

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            ResultSet rs = sr.rs;
            if (rs.next()) {
                VipEnumBean bean = new VipEnumBean();
                bean.id = rs.getInt("id");
                bean.vip_type = rs.getInt("vip_type");
                bean.vip_name = rs.getString("vip_name");
                bean.vip_enable = rs.getBoolean("vip_enable");
                bean.vip_desc = rs.getString("vip_desc");
                bean.price = rs.getFloat("price");
                bean.price_unit = rs.getString("price_unit");
                bean.discount_price = rs.getFloat("discount_price");
                bean.discount_price_unit = rs.getString("discount_price_unit");
                bean.discount_enable = rs.getBoolean("discount_enable");
                bean.product_code = rs.getString("product_code");
                bean.vip_view_color = rs.getString("vip_view_color");
                bean.vip_duration = rs.getLong("vip_duration");
                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

}
